<template>
	<view>
		<view>
			<view class="flex align-center justify-between padding-lg bg-white margin-top-xs">
				<view class="text-black">头像</view>
				<view class="avatar">
					<image :src="profile.avatar" class="w-100" mode="widthFix"></image>
				</view>
			</view>

			<view class="margin-top-sm bg-white">
				<view class="padding-lr-lg padding flex justify-between" @click="changeProfile('username', 10)">
					<view class="text-black">用户名</view>
					<view class="text-grey content">
						<text>{{profile.username}}</text>
						<text class="padding-left-xs"></text>
					</view>
				</view>
				<view class="flex padding-lr-lg padding flex justify-between border-top" @click="changeProfile('bio',50)">
					<view class="text-black">个性签名</view>
					<view class="text-grey content">{{profile.bio || "未填写"}}</view>
				</view>
			</view>
			<view class="flex margin margin-top-xl">
				<button class="w-100" type="primary" size="mini" :disabled="!hasChanged" @click="submitChange">保存</button>
			</view>
		</view>
		<uni-popup ref="popup" type="bottom" :animation="true">
			<view class="padding bg-white">
				<uni-easyinput type="textarea" v-model="inputValue" :maxlength="inputMaxlen"></uni-easyinput>
				<view class="flex w-100 justify-end align-center padding-top">
					<view class="margin-right-sm">{{inputMaxlen - inputValue.length}}</view>
					<view><button size="mini" type="primary" @click="changeProfileSave"
							:disabled="inputValue.length==0 || inputValue == inputOld">确定</button></view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
import {mapGetters} from 'vuex';
	export default {
		data() {
			return {
				profile: {
					username: "",
					bio: "",
          avatar:""
				},
				hasChanged: false,
				inputValue: "",
				inputMaxlen: 50,
				inputOld: "",
				changeProfileSave: null
			}
		},
    computed: {
      ...mapGetters({
        user: 'user/getUserinfo'
      }),
    },
    onShow(){
      this.profile = this.user;
    },
		methods: {
			open() {
				this.$refs.popup.open()
			},
			changeProfile(key, maxlen) {
				console.log("click")
				this.inputOld = this.profile[key];
				this.inputValue = this.inputOld;
				this.inputMaxlen = maxlen ?? 50;
				this.changeProfileSave = () => {
					this.hasChanged = true
					this.profile[key] = this.inputValue
					this.$refs.popup.close()
				}
				this.open();
			},
      submitChange(){
        this.$api.changeProfile({
          avatar:this.profile.avatar,
          username:this.profile.username,
          bio:this.profile.bio
        }).then(res=>{
          console.log(res)
          this.$store.commit("user/setUserinfo", this.profile)
          this.pro
        })
      }
		},
		mounted() {

		}
	}
</script>

<style lang="scss">
	.avatar {
		width: 100upx;

		image {
			border-radius: 500upx;
		}
	}

	.border-top {
		border-top: 5upx solid rgb(245, 245, 245);
	}

	.content {
		height: 100%;
		max-width: calc(100% - 6em);
		display: -webkit-box;
		overflow: hidden;
		word-break: break-all;
		text-overflow: ellipsis;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 1;

	}
</style>
